package com.mrd.weixinpay.util;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;

public class ChangeUtil {

	/**
	 * 将参数直接转换成Dto
	 * @author han
	 * @param dtoClass
	 *            dto Class对象
	 * @return
	 * @return Object
	 */
	public <T> T getDto(Class dtoClass,HttpServletRequest request) {
		Map orig = new HashMap();
		Object invokeTester = null;
		Map parameMap = request.getParameterMap();
		for(Object key : parameMap.keySet()) {
			Object obj = parameMap.get(key);
			if(obj instanceof String[]) {
				String[] strs = (String[]) obj;
				orig.put(key, strs[0]);
			} else {
				orig.put(key, obj);
			}
		}
		try {
			invokeTester = dtoClass.newInstance();
			copyProperties(invokeTester, orig);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			invokeTester = null;
		}
		return (T) invokeTester;
	}

	public void copyProperties(Object target,Object source) throws IllegalAccessException,
	        InvocationTargetException {
		BeanUtils.copyProperties(target, source);
	}

	public void copyProperties(Class destClass,Object orig) throws IllegalAccessException,
	        InvocationTargetException {
		BeanUtils.copyProperties(destClass, orig);
	}

	/**
	 * 计算时间差
	 * @param from
	 *            大时间
	 * @param to
	 *            小时间
	 * @return 分钟数
	 */
	public static long getTimeInterval(Date from,Date to) {
		long interval = 0;
		interval = from.getTime() - to.getTime();
		interval = interval / (1000 * 60);
		return interval;

	}

}
